/*
 * Project Name: jmax
 * File Name: FilterTranslator.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.sys.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.xdtech.common.utils.JsonUtil;
import com.xdtech.core.model.BaseModel;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-3-18 下午2:24:36
 */
@Entity
@Table(name="SYS_FILTER_TRANSLATOR")
public class FilterTranslator extends BaseModel implements Serializable {
	private static final long serialVersionUID = -7719892259460672823L;

	@Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Column(name = "TRANSLATOR_TEXT")
	@Lob
	private String text;
	
	@Column(name = "TRANSLATOR_XML")
	@Lob
	private String xml;
	
	@Column(name = "RESOURCE_ID")
	private Long resouceId;
	
	@OneToMany(mappedBy="translator")
	private List<FilterGroup> groups = new ArrayList<FilterGroup>();
	@OneToMany(mappedBy="translator")
	private List<FilterParam> params = new ArrayList<FilterParam>();
	
	public FilterTranslator() {
		super();
	}
	
	public FilterTranslator(Long id) {
		super();
		this.id = id;
	}

	public Long getId()
	{
		return id;
	}
	public void setId(Long id)
	{
		this.id = id;
	}
	public String getText()
	{
		return text;
	}
	public void setText(String text)
	{
		this.text = text;
	}
	

	public String getXml()
	{
		return xml;
	}

	public void setXml(String xml)
	{
		this.xml = xml;
	}

	public Long getResouceId()
	{
		return resouceId;
	}
	public void setResouceId(Long resouceId)
	{
		this.resouceId = resouceId;
	}
	public List<FilterGroup> getGroups()
	{
		return groups;
	}
	public void setGroups(List<FilterGroup> groups)
	{
		this.groups = groups;
	}
	public List<FilterParam> getParams()
	{
		return params;
	}
	public void setParams(List<FilterParam> params)
	{
		this.params = params;
	}
	
	public void createText()
	{
		StringBuffer sb = new StringBuffer("");
		for (FilterGroup group : groups)
		{
			sb.append(" "+group.getOption()+" (");
			List<FilterRule> rules = group.getFilterRules();
			for (FilterRule rule : rules)
			{
				if (rule.getOption()!=null) {
					sb.append(" "+rule.getOption()+" ");
				}
				if ("eq".equals(rule.getType()))
				{
					sb.append(rule.getField()+"="+rule.getValue());
				}else if ("in".equals(rule.getType()))
				{
					sb.append(rule.getField()+" in ("+rule.getValue()+")");
				}
			}
			sb.append(")");
		}
		this.text = sb.toString();
	}
	

}
